<?php
	/**
	* @package admin
	* @desc This php file is used to new add parks to the database
	*/
	
	session_start();
	
	// Direct calling check
	if (!isset($_SESSION["user_id"]))
	{
		header('Location: ../index.php');
		exit();
	}
	if ($_SESSION["role"] != "admin")
	{
		header('Location: ../index.php');
		exit();
	}

	/**
	* @desc This include file will load the directory settings for the Smarty Templates
	*/
	require '../includes/smarty.inc';
	$smarty->assign('title', "Add Park");
	$smarty->assign('user_id', $_SESSION["user_id"]);	
	$smarty->assign('last', $_SESSION["lastviewed"]);
	$smarty->assign('firstname', $_SESSION["firstname"]);
	$smarty->assign('role', $_SESSION["role"]);
	$smarty->assign('top5', $_SESSION["top5"]);	
	$smarty->assign('pathdepth', "../");
	$smarty->display('header.tpl');
	
	/**
	* @desc This include file connects to the database.
	*/
	require '../includes/connect.inc';
	/**
	* @desc This include file will load the functions that are required to call 
	*		from the database
	*/
	require '../includes/db_functions.inc';
	
	// Suburbs **********************************
	$suburbs = getSuburbs(); // Get suburbs info
	$arSuburbs = array();
	
	if ($suburbs == FALSE)
		//arSuburbs = array('postcode' => 'error', 'name' => 'Failed to Load');
		$smarty->assign('failPC', 'Fail to load postcodes');
	else						
		//Iterate through results, extracting suburb name and postcode		
		while($row = mysql_fetch_array($suburbs))
			$arSuburbs[] = array('id' => $row["suburb_id"], 'suburb' => $row["name"], 'postcode' => $row["postcode"]);
		
	$smarty->assign('optionsPC', $arSuburbs);	
	
	
	

	//get different equipment for parks

	$result = getDistinctEquip();

	if ($result == FALSE)
		die("Could not load equipment");
	
	$numrows = mysql_num_rows($result);
	
	$parkEq = array();
	for($i = 1; $i <= $numrows; $i++)
	{
		$row = mysql_fetch_array($result);
		
		if(isset($_SESSION["form"][$i]))
			$checked = true;
		
		else		
			$checked = false;
		
		$parkEq[] = array('id' => $row["equip_id"], 'name' => $row["name"], 'checked' => $checked);
	}	
	$smarty->assign('parkEq', $parkEq);
	
	
	
	if(isset($_SESSION["errors"]["name"]))
		$smarty->assign('errorsname', $_SESSION["errors"]["name"]);
	else
		$smarty->assign('name', $_SESSION["form"]["name"]);
	
	if(isset($_SESSION["errors"]["address"]))
		$smarty->assign('errorsaddress', $_SESSION["errors"]["address"]);
	else
		$smarty->assign('address', $_SESSION["form"]["address"]);
	
	if(isset($_SESSION["errors"]["type"]))
		$smarty->assign('errorstype', $_SESSION["errors"]["type"]);
	else
		$smarty->assign('type', $_SESSION["form"]["type"]);
	
	if(isset($_SESSION["errors"]["pic"]))
		$smarty->assign('errorspic', $_SESSION["errors"]["pic"]);
	
	$errors = array();
	$form = array();
	for($i = 0; $i < $numrows; $i++)
	{
		if(isset($_SESSION["errors"][$i+1]))
			$errors[$i] = $_SESSION["errors"][$i+1];
		else
			$form[$i] = $_SESSION["form"][$i+1];
	}
	
	$smarty->assign('errors', $errors);
	$smarty->assign('form', $form);
	$smarty->display('admin/addpark.tpl');
	
	mysql_close($connection); //Disconnet
	
	unset($_SESSION["errors"]);
	unset($_SESSION["form"]);	
	
	
	$smarty->assign('pathdepth', "../");
	$smarty->display('footer.tpl');
	
	
?>